package org.jetlinks.pro.network.http;

import io.vertx.core.Vertx;
import lombok.SneakyThrows;
import org.jetlinks.pro.gateway.session.DefaultDeviceSessionManager;
import org.jetlinks.pro.network.ClusterNetworkManager;
import org.jetlinks.pro.network.DefaultNetworkType;
import org.jetlinks.pro.network.NetworkManager;
import org.jetlinks.pro.network.NetworkProperties;
import org.jetlinks.pro.network.NetworkProvider;
import org.jetlinks.pro.network.http.client.HttpClientProvider;
import org.jetlinks.pro.network.http.device.HttpServerDeviceGatewayProvider;
import org.jetlinks.pro.network.http.server.vertx.HttpServerProvider;
import org.jetlinks.pro.network.security.Certificate;
import org.jetlinks.pro.network.security.DefaultCertificate;
import org.jetlinks.supports.event.BrokerEventBus;
import org.jetlinks.supports.test.InMemoryDeviceRegistry;
import org.jetlinks.supports.test.MockProtocolSupport;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;
import reactor.core.publisher.Mono;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class HttpTestUtils {

    @SneakyThrows
    public static Certificate createServerCertificate(String id){
        DefaultCertificate serverCert = new DefaultCertificate(id, "test");
        byte[] serverKs = StreamUtils.copyToByteArray(new ClassPathResource("server.pem").getInputStream());
        byte[] serverCer = StreamUtils.copyToByteArray(new ClassPathResource("server.csr").getInputStream());

        byte[] trust = StreamUtils.copyToByteArray(new ClassPathResource("trustStore.pem").getInputStream());

        serverCert.initPemKey(Collections.singletonList(serverKs), Collections.singletonList(serverCer))
                  .initPemTrust(Collections.singletonList(trust));

        return serverCert;
    }


    @SneakyThrows
    public static Certificate createClientCertificate(String id){
        DefaultCertificate clientCert = new DefaultCertificate(id, "test");

        byte[] clientKs = StreamUtils.copyToByteArray(new ClassPathResource("client.pem").getInputStream());
        byte[] clientCer = StreamUtils.copyToByteArray(new ClassPathResource("client.csr").getInputStream());

        byte[] trust = StreamUtils.copyToByteArray(new ClassPathResource("trustStore.pem").getInputStream());

        clientCert.initPemKey(Collections.singletonList(clientKs), Collections.singletonList(clientCer))
                  .initPemTrust(Collections.singletonList(trust));

        return clientCert;
    }

}
